package com.jues.huichain.login.activity;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.jakewharton.rxbinding2.view.RxView;
import com.jues.huichain.R;
import com.jues.huichain.R2;
import com.jues.huichain.login.entity.RegisterBusinessListEntity;
import com.jues.huichain.login.entity.RegisterDepartmentListEntity;
import com.jues.huichain.login.entity.RegisterPositionListEntity;
import com.jues.huichain.login.entity.RegisterRequest;
import com.jues.huichain.login.presenter.Register2Presenter;
import com.lm.sjy.titlebar.widget.CommonTitleBar;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.util.ActivityControl;
import health.lm.com.router.ARouterConstant;

/**
 * Created by Android Studio .
 * Project：hlian com.jues.huichain.login.activity
 * Date：2019/8/7 9:04
 * Introduce：hlian
 */
@Route(path = ARouterConstant.Register2Activity)
public class RegisterActivity2 extends XActivity<Register2Presenter> {
    @Autowired
    public RegisterRequest data;
    @BindView(R2.id.title_bar)
    CommonTitleBar mTitleBar;
    @BindView(R2.id.textView22)
    TextView mTextView22;
    @BindView(R2.id.textView24)
    TextView mTextView24;
    @BindView(R2.id.textView25)
    TextView mTextView25;
    @BindView(R2.id.textView26)
    TextView mTextView26;
    @BindView(R2.id.textView27)
    TextView mTextView27;
    @BindView(R2.id.textView28)
    TextView mTextView28;
    @BindView(R2.id.textView29)
    EditText mTextView29;
    @BindView(R2.id.textView30)
    TextView mTextView30;
    @BindView(R2.id.editText)
    EditText mEditText;
    @BindView(R2.id.textView33)
    TextView mTextView33;
    @BindView(R2.id.textView34)
    TextView mTextView34;
    @BindView(R2.id.textView35)
    TextView mTextView35;
    @BindView(R2.id.textView36)
    TextView mTextView36;
    @BindView(R2.id.textView31)
    TextView mTextView31;
    private ActivityControl mControl;

    @Override
    public int getLayoutId() {
        return R.layout.activity_register2;
    }

    @Override
    public Register2Presenter newP() {
        return new Register2Presenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mControl = new ActivityControl();
        mControl.addRegisterActivity(this);
        mTitleBar.setListener((v, action, extra) -> finish());
        onClick();
        String login = "已有账号？去<font color='#cf9155'>登录</font>";
        String register = "注册代表你已同意 <font color='#cf9155'>会链用户协议、隐私政策</font>";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            mTextView36.setText(Html.fromHtml(login, Html.FROM_HTML_MODE_LEGACY));
            mTextView31.setText(Html.fromHtml(register, Html.FROM_HTML_MODE_LEGACY));
        } else {
            mTextView36.setText(Html.fromHtml(login));
            mTextView31.setText(Html.fromHtml(register));
        }
    }

    @SuppressLint("CheckResult")
    private void onClick() {
        //String[] data = new String[]{"b", "c", "D", "E", "b", "c", "D", "E"};
        RxView.clicks(mTextView22).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            //new XPopup.Builder(context).asCenterList("请选择职务", data, (position, text) -> {
            //    mTextView22.setText(text);
            //    this.data.setPosition(text);
            //}).show();
            getP().getPositionData();
        });
        RxView.clicks(mTextView25).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            //new XPopup.Builder(context).asCenterList("请选择部门", data, (position, text) -> {
            //    mTextView25.setText(text);
            //    this.data.setDepartment(text);
            //}).show();
            getP().getDepartmentData();
        });
        RxView.clicks(mTextView27).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            //new XPopup.Builder(context).asCenterList("请选择行业", data, (position, text) -> {
            //    mTextView27.setText(text);
            //    this.data.setBusiness(text);
            //}).show();
            getP().getBusinessData(this.data.getParent_mobile());
        });
        RxView.clicks(mTextView34).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            //上一步
            finish();
        });
        RxView.clicks(mTextView35).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            //注册
            this.data.setServiceProject(mTextView29.getText().toString());
            this.data.setIntroduction(mEditText.getText().toString());
            getP().register(this.data);
        });
        RxView.clicks(mTextView36).throttleFirst(1, TimeUnit.SECONDS).subscribe(b -> {
            gotoActivity(ARouterConstant.LoginActivity);
            mControl.closeRegisterActivity();
        });
    }

    public void registerSuccess(String msg) {
        getvDelegate().toastShort(msg);
        gotoActivity(ARouterConstant.LoginActivity);
        mControl.closeRegisterActivity();
        //finish();
    }

    @SuppressLint("CheckResult")
    public void setPositionData(List<RegisterPositionListEntity.DutiesListEntity> pList) {
        List<String> title = new ArrayList<>();
        for (RegisterPositionListEntity.DutiesListEntity entity : pList) title.add(entity.getTitle());
        String[] data = title.toArray(new String[pList.size()]);
        new XPopup.Builder(context).asCenterList("请选择职务", data, (position, text) -> {
            mTextView22.setText(text);
            for (RegisterPositionListEntity.DutiesListEntity entity : pList){
                if (text.equals(entity.getTitle())) this.data.setPosition(entity.getId());
            }
        }).show();
    }

    @SuppressLint("CheckResult")
    public void setDepartmentData(List<RegisterDepartmentListEntity.DepartmentListEntity> pList) {
        List<String> title = new ArrayList<>();
        for (RegisterDepartmentListEntity.DepartmentListEntity entity : pList) title.add(entity.getTitle());
        String[] data = title.toArray(new String[pList.size()]);
        new XPopup.Builder(context).asCenterList("请选择部门", data, (position, text) -> {
            mTextView25.setText(text);
            for (RegisterDepartmentListEntity.DepartmentListEntity entity : pList){
                if (text.equals(entity.getTitle())) this.data.setDepartment(entity.getId());
            }
        }).show();
    }

    @SuppressLint("CheckResult")
    public void setBusinessData(List<RegisterBusinessListEntity.IndustryListEntity> pList) {
        List<String> title = new ArrayList<>();
        for (RegisterBusinessListEntity.IndustryListEntity entity : pList) title.add(entity.getTitle());
        String[] data = title.toArray(new String[pList.size()]);
        new XPopup.Builder(context).asCenterList("请选择行业", data, (position, text) -> {
            mTextView27.setText(text);
            for (RegisterBusinessListEntity.IndustryListEntity entity : pList){
                if (text.equals(entity.getTitle())) this.data.setBusiness(entity.getId());
            }
        }).show();
    }
}
